package io.zcoder.innoway.widgets;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.zcoder.innoway.R;
import io.zcoder.innoway.commons.Constants;
import io.zcoder.innoway.utils.Utils;


public class ScrollSelectWeightView extends LinearLayout implements View.OnTouchListener {

    private static final int DURATION_SCROLL = 300;

    @BindView(R.id.scrollView)
    HorizontalScrollView mScrollView;
    @BindView(R.id.layout_date)
    LinearLayout mLayoutDateView;

    List<LinearLayout> mLayoutItemDate = new ArrayList<>();
    List<TextViewCustom> mTextDates = new ArrayList<>();

    private Point mPoint = new Point();
    private int mCurrentIndex = 20;
    private List<String> mCurrentValues;
    private SelectCallBack mSelectCallBack;

    public ScrollSelectWeightView(Context context) {
        super(context);
    }

    public ScrollSelectWeightView(Context context, AttributeSet attrs) {
        super(context, attrs);
        View view = LayoutInflater.from(getContext()).inflate(R.layout.layout_date_picker_custom, this, true);
        ButterKnife.bind(view);
        WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        wm.getDefaultDisplay().getSize(mPoint);
//        mPoint.set(mPoint.x - 2 * (int) getResources().getDimension(R.dimen.dp20), mPoint.y);

    }

    public void initData(float goal, int unit, SelectCallBack callBack) {
        mSelectCallBack = callBack;
        mCurrentValues = new ArrayList<>();
        int indexGoal = 0;
        for (float i = Constants.SELECT_WEIGHT_FROM; i <= Constants.SELECT_WEIGHT_TO; i += 0.5f) {
            mCurrentValues.add((Utils.formatFloat(i * Utils.getRatioWeight(unit))) + "");
            if (goal > 0 && i >= goal && indexGoal >= 0) {
                mCurrentIndex = indexGoal;
                indexGoal = -1;
            }
            if (indexGoal >= 0) {
                indexGoal++;
            }
        }
        initViews();
    }

    private void initViews() {
        LayoutParams params = new LayoutParams(mPoint.x / 2 - 1, 10);
        mLayoutDateView.removeAllViews();
        mTextDates = new ArrayList<>();
        mLayoutItemDate = new ArrayList<>();
        mLayoutDateView.addView(new View(getContext()), params);
        for (int i = 0; i < mCurrentValues.size(); i++) {
            View view = View.inflate(getContext(), R.layout.item_select_weight, null);
            mLayoutDateView.addView(view);
            LinearLayout layout = (LinearLayout) view.findViewById(R.id.layout_item_date);
            TextViewCustom textViewCustom = (TextViewCustom) view.findViewById(R.id.text_date);
            mTextDates.add(textViewCustom);
            mLayoutItemDate.add(layout);

            mTextDates.get(i).setText(mCurrentValues.get(i));
        }
        mLayoutDateView.addView(new View(getContext()), params);

        mScrollView.setOnTouchListener(this);
        scrollToPoint(false);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            scrollToPoint(true);
            return true;
        }
        return false;
    }

    private void scrollToPoint(boolean auto) {
        for (int i = 0; i < mCurrentValues.size(); i++) {
            int[] location = {0, 0};
            mLayoutItemDate.get(i).getLocationOnScreen(location);
            if ((!auto && mCurrentIndex == i) || (location[0] <= mPoint.x / 2 && location[0] + getResources().getDimension(R.dimen.width_item_weight) >= mPoint.x / 2)) {
                mTextDates.get(i).setTextColor(Color.WHITE);

                float positionScroll = getResources().getDimension(R.dimen.width_item_weight) * (i + 0.5f) - mPoint.x / 2 + mPoint.x / 2 - 1;
                ObjectAnimator animator =
                        ObjectAnimator.ofInt(mScrollView, "scrollX", (int) positionScroll);
                animator.setDuration(DURATION_SCROLL);
                animator.start();
                mCurrentIndex = i;

                if (mSelectCallBack != null) {
                    mSelectCallBack.onSelected();
                }

            } else {
                mTextDates.get(i).setTextColor(getResources().getColor(R.color.gray63));
            }
        }
    }

    public String getValue() {
        return mCurrentValues.get(mCurrentIndex);
    }

    public int getIndex() {
        return mCurrentIndex + 1;
    }

    public interface SelectCallBack {
        void onSelected();
    }
}
